﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using SanctusSoftware.CcliSongSelectLib.Management;

namespace SanctusSoftware.CcliSongSelectImporter
{
    /// <summary>
    /// Interaction logic for SettingsWindow.xaml
    /// </summary>
    public partial class SettingsWindow : Window, INotifyPropertyChanged
    {

        #region Events

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region Properties

        private Settings _Settings = null;
        
        public Settings Settings
        {
            get { return _Settings; }
            set
            {
                _Settings = value;
                OnPropertyChanged("Settings");
            }
        }

        #endregion

        #region Constructor

        public SettingsWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        #endregion

        private void _OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            this.Close();
        }

        private void _CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void _AddLibraryButton_Click(object sender, RoutedEventArgs e)
        {
            Library library = new Library();
            library.IncludeSubdirectories = true;
            LibraryWindow w = new LibraryWindow(library);
            w.Owner = this;
            w.LibraryValidation = delegate(Library item)
            {
                List<string> errors = new List<string>();
                if (Settings.Libraries.FirstOrDefault(existingLib => existingLib.Path == item.Path) != null)
                {
                    errors.Add(string.Format("A library already exists with the path '{0}'", item.Path));
                }
                return errors.ToArray();
            };
            if (w.ShowDialog() == true)
            {
                this.Settings.Libraries.Add(library);
            }
        }

        private void _RemoveLibraryButton_Click(object sender, RoutedEventArgs e)
        {
            if (_LibraryListBox.SelectedItem == null)
            {
                MessageBox.Show("No Library selected");
            }
            else
            {
                Library lib = _LibraryListBox.SelectedItem as Library;
                if (lib != null)
                {
                    Settings.Libraries.Remove(lib);
                }
            }
        }
    }
}
